package com.itheima.text6;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ServerThread implements Runnable {
    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {

            BufferedInputStream input = new BufferedInputStream(socket.getInputStream());

            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("day12_socket\\" + uuid + ".jpg"));
            // 读写操作
            int len;
            byte[] bytes = new byte[1024];
            while ((len = input.read(bytes)) != -1) {
                output.write(bytes, 0, len);
            }

            socket.shutdownInput();

            output.close();


            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
            writer.println("上传成功");

            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



